import {Component, OnInit} from '@angular/core';
import {BasePage} from '../../common/basePage';
import {Events, NavController, ToastController} from '@ionic/angular';
import {AppConfig} from '../../common/app-config.enum';
import {HttpService} from '../../services/http.service';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {ActivatedRoute} from '@angular/router';
import {UrlAndOPT} from '../../common/url-and-opt.enum';
import {requiredZero} from '../../common/validators';
import * as moment from 'moment';

@Component({
  selector: 'app-membership-info-enter',
  templateUrl: './membership-info-enter.page.html',
  styleUrls: ['./membership-info-enter.page.scss'],
})
export class MembershipInfoEnterPage extends BasePage implements OnInit {

  optionMap: any = {}; // 问题待选项
  userInfo: any = {}; // 用户答案信息
  formModel: FormGroup;
  nowDate = moment().format('YYYY');
  maxDate = '2099-12-31';

  constructor(
    public navCtrl: NavController,
    public toastCtrl: ToastController,
    private fb: FormBuilder,
    private events: Events,
    private routeInfo: ActivatedRoute,
    private service$: HttpService
  ) {
    super(navCtrl, toastCtrl);
    this.formModel = fb.group({
      entLegalName: ['', [Validators.required]],
      entLegalId: ['', [Validators.required]],
      enterpriseScale: [0, [requiredZero]],
      industry: [0, [requiredZero]],
      entRegAmt: [0, [requiredZero]],
      entRegDate: [0, [requiredZero]],
      expireDate: [0, [requiredZero]],
      entPhone: [0, [requiredZero]],
      entFax: [0, [requiredZero]],
      entAddr: ['', [Validators.required]]
    });
  }

  ngOnInit() {
    this.routeInfo.data
      .subscribe(resolve => {
        if (resolve['data']) {
          console.log(resolve['data']);
          this.optionMap = resolve['data'];
          // 问题答案
          this.userInfo = resolve['data']['entUserInfo'];
          setTimeout(() => {
            this.formModel.patchValue({
              entLegalName: this.userInfo['entLegalName'],
              entLegalId: this.userInfo['entLegalId'],
              enterpriseScale: this.userInfo['enterpriseScale'],
              industry: this.userInfo['industry'],
              entRegAmt: '' + this.userInfo['entRegAmt'], // 数字转字符串
              entRegDate: this.userInfo['entRegDate'],
              expireDate: this.userInfo['expireDate'],
              entPhone: this.userInfo['entPhone'],
              entFax: this.userInfo['entFax'],
              entAddr: this.userInfo['addr'] || ''
            });
          }, 0);
        } else {
          this.goBack();
        }
      });
  }

  doSave() {
    if (this.formModel.valid) {
      this.service$.doRequest(UrlAndOPT.auth_membership_enter_submit, this.formModel.value)
        .subscribe(obj => {
          if (obj) {
            // 发出流程变化通知
            this.events.publish(AppConfig.TOPIC_UNDO_LIST);
            this.showToast('保存成功');
            this.goBack();
          }
        });
    } else {
      this.showToast('请完善信息后保存');
    }
  }

}
